//
//  BCDebugFpsConfig.m
//  Pod
//
//  Created by Basic on 2017/3/14.
//  Copyright © 2017年 naruto. All rights reserved.
//

#import "BCDebugFpsConfig.h"
#import "BCDebugAppMonitor.h"
#import <objc/message.h>

/// fps 关闭开关，0：否，1：是，默认显示fps
#define kBCDebugFpsConfig_Off   @"debug_fps"
/// ram 关闭开关，0：否，1：是，默认显示ram
#define kBCDebugRamConfig_Off   @"debug_ram"
/// cpu 关闭开关，0：否，1：是，默认显示cpu
#define kBCDebugCpuConfig_Off   @"debug_cpu"

@implementation BCDebugFpsConfig
#pragma mark - system


#pragma mark -  BCDebugKitProtocol
+ (void)bcDebugWillLoad {
    NSInteger fpsOff = [[[NSUserDefaults standardUserDefaults] valueForKey:kBCDebugFpsConfig_Off] integerValue];
    if (fpsOff<1) {
        [[BCDebugAppMonitor sharedInstance] startFpsMonitor];
    }
    NSInteger ramOff = [[[NSUserDefaults standardUserDefaults] valueForKey:kBCDebugRamConfig_Off] integerValue];
    if (ramOff<1) {
        [[BCDebugAppMonitor sharedInstance] startRamMonitor];
    }
    NSInteger cpuOff = [[[NSUserDefaults standardUserDefaults] valueForKey:kBCDebugCpuConfig_Off] integerValue];
    if (cpuOff<1) {
        [[BCDebugAppMonitor sharedInstance] startCpuMonitor];
    }
    [[BCDebugAppMonitor sharedInstance] loadAppStartTime];
}
-(BCDebugItemGroup *)bcDebugConfigSetupDebugGroup {
    BCDebugItemGroup *debugGroup = [BCDebugItemGroup new];
    debugGroup.bcName = @"性能监控";
    debugGroup.bcConfigClass = [self class];
    //启动耗时
    BCDebugItemModel *itemSwitch = [BCDebugItemModel new];
    itemSwitch.bcName = @"启动耗时";
    itemSwitch.bcDetail = [NSString stringWithFormat:@"大概%.2fs",[BCDebugAppMonitor sharedInstance].startTime/1000.0];
    itemSwitch.bcAccessoryType = UITableViewCellAccessoryNone;
    itemSwitch.bcSelectStyle = UITableViewCellSelectionStyleNone;
    [debugGroup.bcItems addObject:itemSwitch];
    //fps
    NSInteger fpsOff = [[[NSUserDefaults standardUserDefaults] valueForKey:kBCDebugFpsConfig_Off] integerValue];
    itemSwitch = [BCDebugItemModel new];
    itemSwitch.bcName = @"屏幕刷新FPS";
    itemSwitch.bcDetail = (fpsOff > 0)?@"已关闭":@"已打开，悬浮显示";
    itemSwitch.bcSelectOptionIndex = (fpsOff>0)?1:0;
    itemSwitch.bcType = BCDebugItemType_Selector;
    itemSwitch.bcAccessoryType = UITableViewCellAccessoryDisclosureIndicator;
    itemSwitch.selectOptionAction = ^(BCDebugItemModel *item, BCDebugItemOption *option, NSInteger selectRow){
        //保存上次选择的索引和 环境值
        [[NSUserDefaults standardUserDefaults] setValue:@(option.bcTag) forKey:kBCDebugFpsConfig_Off];
        [[NSUserDefaults standardUserDefaults] synchronize];
        if (option.bcTag>0) {
            //打开
            [[BCDebugAppMonitor sharedInstance] startFpsMonitor];
        }
        else {
            //关闭
            [[BCDebugAppMonitor sharedInstance] stopFpsMonitor];
        }
    };
    BCDebugItemOption *debugOption1 = [BCDebugItemOption new];
    debugOption1.bcName = @"打开";
    debugOption1.bcTag = 0;
    [itemSwitch.bcOptions addObject:debugOption1];
    debugOption1 = [BCDebugItemOption new];
    debugOption1.bcName = @"关闭";
    debugOption1.bcTag = 1;
    [itemSwitch.bcOptions addObject:debugOption1];
    [debugGroup.bcItems addObject:itemSwitch];
    
    //ram
    NSInteger ramOff = [[[NSUserDefaults standardUserDefaults] valueForKey:kBCDebugRamConfig_Off] integerValue];
    itemSwitch = [BCDebugItemModel new];
    itemSwitch.bcName = @"内存RAM";
    itemSwitch.bcDetail = (ramOff > 0)?@"已关闭":@"已打开，悬浮显示";
    itemSwitch.bcSelectOptionIndex = (ramOff>0)?1:0;
    itemSwitch.bcType = BCDebugItemType_Selector;
    itemSwitch.bcAccessoryType = UITableViewCellAccessoryDisclosureIndicator;
    itemSwitch.selectOptionAction = ^(BCDebugItemModel *item, BCDebugItemOption *option, NSInteger selectRow){
        //保存上次选择的索引和 环境值
        [[NSUserDefaults standardUserDefaults] setValue:@(option.bcTag) forKey:kBCDebugRamConfig_Off];
        [[NSUserDefaults standardUserDefaults] synchronize];
        if (option.bcTag>0) {
            //打开
            [[BCDebugAppMonitor sharedInstance] startRamMonitor];
        }
        else {
            //关闭
            [[BCDebugAppMonitor sharedInstance] stopRamMonitor];
        }
    };
    debugOption1 = [BCDebugItemOption new];
    debugOption1.bcName = @"打开";
    debugOption1.bcTag = 0;
    [itemSwitch.bcOptions addObject:debugOption1];
    debugOption1 = [BCDebugItemOption new];
    debugOption1.bcName = @"关闭";
    debugOption1.bcTag = 1;
    [itemSwitch.bcOptions addObject:debugOption1];
    [debugGroup.bcItems addObject:itemSwitch];
    
    //cpu使用率
    NSInteger cpuOff = [[[NSUserDefaults standardUserDefaults] valueForKey:kBCDebugCpuConfig_Off] integerValue];
    itemSwitch = [BCDebugItemModel new];
    itemSwitch.bcName = @"CPU使用率";
    itemSwitch.bcDetail = (cpuOff > 0)?@"已关闭":@"已打开，悬浮显示";
    itemSwitch.bcSelectOptionIndex = (cpuOff>0)?1:0;
    itemSwitch.bcType = BCDebugItemType_Selector;
    itemSwitch.bcAccessoryType = UITableViewCellAccessoryDisclosureIndicator;
    itemSwitch.selectOptionAction = ^(BCDebugItemModel *item, BCDebugItemOption *option, NSInteger selectRow){
        //保存上次选择的索引和 环境值
        [[NSUserDefaults standardUserDefaults] setValue:@(option.bcTag) forKey:kBCDebugCpuConfig_Off];
        [[NSUserDefaults standardUserDefaults] synchronize];
        if (option.bcTag>0) {
            //打开
            [[BCDebugAppMonitor sharedInstance] startCpuMonitor];
        }
        else {
            //关闭
            [[BCDebugAppMonitor sharedInstance] stopCpuMonitor];
        }
    };
    debugOption1 = [BCDebugItemOption new];
    debugOption1.bcName = @"打开";
    debugOption1.bcTag = 0;
    [itemSwitch.bcOptions addObject:debugOption1];
    debugOption1 = [BCDebugItemOption new];
    debugOption1.bcName = @"关闭";
    debugOption1.bcTag = 1;
    [itemSwitch.bcOptions addObject:debugOption1];
    [debugGroup.bcItems addObject:itemSwitch];
    
    return debugGroup;
}

@end
